<?php
namespace app\api;


use app\lib\exception\BaseException;
use think\exception\Handle;
use think\Response;
use Throwable;


/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        // 添加自定义异常处理机制
        if ($e instanceof BaseException) {
            $code = $e->code;
            $msg = $e->msg;
            $errorCode = $e->errorCode;
        }else{
            if(env('app_debug') and $this->app->isDebug()){
                return parent::render($request,$e);
            }


            $code = 500;
            $msg = 'sorry，we make a mistake. (^o^)Y';
            $errorCode = 999;
//            $this->recordErrorLog($e);
        }

        $result = [
            'msg'  => $msg,
            'error_code' => $errorCode,
            'request_url' => request()->url()
        ];
        return json($result, $code);
        // 其他错误交给系统处理
//        return parent::render($request, $e);
    }
}
